#include "Main/stdafx.h"
#include "JDNNGameScence.h"
#include "JDNNPlayer.h"
#include "Game/GameLib.h"
#include "CMD_Ox.h"
#include "JDNNPlayer.h"

class JDNNTouchScence
	:public cocos2d::Layer
{
public:
	virtual bool init()
	{
		if ( !cocos2d::Layer::init() )
		{
			return false;
		}
		setTouchEnabled(true);
		setTouchMode(cocos2d::Touch::DispatchMode::ONE_BY_ONE);
		return true;
	}
public:
	CREATE_FUNC(JDNNTouchScence);
public:
	virtual bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent)
	{
		if (!utility::isAllVisible(this))
		{
			return false;
		}
		if (pTouch->getID() != 0)
		{
			return false;
		}
		return JDNNGameScence::Instance().ccTouchBegan(pTouch->getLocation());
	}
};

void JDNNGameScence::initTouch()
{
	JDNNTouchScence* pTouchScence = new JDNNTouchScence;
	pTouchScence->init();
	addChild(pTouchScence);
}

bool JDNNGameScence::ccTouchBegan(cocos2d::Vec2 kPos)
{
	if (!isVisible())
	{
		return false;
	}
	if (_layout->getChildByName("game_niuniu_panel")->isVisible() && getGameState() == JDNN_STATE_PLAYING)
	{
		m_pTouchCardNode = m_pLocal->getTouchCardNode(kPos);
		if (m_pTouchCardNode)
		{
			CCardSprite* currCard = (CCardSprite*)m_pTouchCardNode;
			if (currCard->getStatus() == CCardSprite::ST_UNSELECTED && m_pLocal->getSelectedCardNum() >= 3)
			{
				return false;
			}
			currCard->selected();
			m_pLocal->refreshNiuPanel(_layout->getChildByName("game_niuniu_panel"));
			return true;
		}
	}
	return false;
}